export const initCharts = function(data) {
  AmCharts.ready(function () {
    var chart;
    var chartData = [];
    var chartCursor;
    chart = new AmCharts.AmSerialChart();
    //console.log(data)
    chart.categoryField = "date";
    chart.balloon.bulletSize = 5;

    // AXES
    // category


    var categoryAxis = chart.categoryAxis;
    categoryAxis.parseDates = true;
    categoryAxis.minPeriod = "DD";
    categoryAxis.dashLength = 1;
    categoryAxis.minorGridEnabled = true;
    categoryAxis.twoLineMode = false;
    categoryAxis.dateFormats = [{
      period: 'fff',
      format: 'JJ:NN:SS'
    }, {
      period: 'ss',
      format: 'JJ:NN:SS'
    }, {
      period: 'mm',
      format: 'JJ:NN'
    }, {
      period: 'hh',
      format: 'JJ:NN'
    }, {
      period: 'DD',
      format: 'DD'
    }, {
      period: 'WW',
      format: 'DD'
    }, {
      period: 'MM',
      format: 'M月'
    }, {
      period: 'YYYY',
      format: 'YYYY年'
    }];

    categoryAxis.axisColor = "#DDDDDD";

    // value
    var valueAxis = new AmCharts.ValueAxis();
    valueAxis.axisAlpha = 0;
    valueAxis.dashLength = 1;
    chart.addValueAxis(valueAxis);

    // GRAPH
    var graph = new AmCharts.AmGraph();
    graph.type = "smoothedLine";
    graph.valueField = "visits";
    graph.bullet = "round";
    graph.bulletSize = 8;
    graph.bulletBorderColor = "#FFFFFF";
    graph.bulletBorderThickness = 2;
    graph.bulletBorderAlpha = 1;
    graph.lineThickness = 1;
    graph.negativeLineColor = "#f42";
    graph.lineColor="#f00";
    graph.balloonText = "日期:[[category]]<br>指数:<b><span style='font-size:14px;'>[[value]]</span></b>";
    graph.hideBulletsCount = 50;
    chart.addGraph(graph);

    // CURSOR
    var chartCursor = new AmCharts.ChartCursor();
    chart.addChartCursor(chartCursor);

    // SCROLLBAR
    var chartScrollbar = new AmCharts.ChartScrollbar();
    chart.addChartScrollbar(chartScrollbar);

    chart.creditsPosition = "bottom-right";

    // WRITE
    chart.write("chartdiv1");
    $.ajax({
      url: '/Index/get_rhb.html',
      async: false,
      type: 'post',
      success:function(res){
        getData(res,chartData);
        chart.dataProvider = chartData;
        chart.validateNow();

        chart.validateData();
      }
    })

    $('.jgzs_top_nav ul li').click(function () {

      chartData = [];
      var Oindex=$(this).index();
      var type=$(this).attr("data");
      $('.jgzs_top_nav ul li').find('a').removeClass('dqzt');
      $(this).find('a').addClass('dqzt');
      $.ajax({
        url: '/Index/get_rhb.html',
        async: false,
        type: 'post',
        data:{type:type},
        success:function(res){
          getData(res,chartData);
          chart.dataProvider = chartData;
          chart.validateNow();
          chart.validateData();
        }
      })
    });

  });



  $(".close").click(function(){
    $(".dialog").fadeOut();
  })
  //弹窗
  AmCharts.ready(function () {
    var chart;
    var chartData = [];
    var chartCursor;
    chart = new AmCharts.AmSerialChart();

    chart.categoryField = "date";
    chart.balloon.bulletSize = 5;

    // AXES
    // category
    var categoryAxis = chart.categoryAxis;
    categoryAxis.parseDates = true;
    categoryAxis.minPeriod = "DD";
    categoryAxis.dashLength = 1;
    categoryAxis.minorGridEnabled = true;
    categoryAxis.twoLineMode = false;
    categoryAxis.dateFormats = [{
      period: 'fff',
      format: 'JJ:NN:SS'
    }, {
      period: 'ss',
      format: 'JJ:NN:SS'
    }, {
      period: 'mm',
      format: 'JJ:NN'
    }, {
      period: 'hh',
      format: 'JJ:NN'
    }, {
      period: 'DD',
      format: 'DD'
    }, {
      period: 'WW',
      format: 'DD'
    }, {
      period: 'MM',
      format: 'M月'
    }, {
      period: 'YYYY',
      format: 'YYYY年'
    }];

    categoryAxis.axisColor = "#DDDDDD";

    // value
    var valueAxis = new AmCharts.ValueAxis();
    valueAxis.axisAlpha = 0;
    valueAxis.dashLength = 1;
    chart.addValueAxis(valueAxis);

    // GRAPH
    var graph = new AmCharts.AmGraph();
    graph.type = "smoothedLine";
    graph.valueField = "visits";
    graph.bullet = "round";
    graph.bulletSize = 8;
    graph.bulletBorderColor = "#FFFFFF";
    graph.bulletBorderThickness = 2;
    graph.bulletBorderAlpha = 1;
    graph.lineThickness = 1;
    graph.negativeLineColor = "#f42";
    graph.lineColor="#f00";
    graph.balloonText = "日期:[[category]]<br>价格:<b><span style='font-size:14px;'>[[value]]</span></b>元/公斤";
    graph.hideBulletsCount = 50;
    chart.addGraph(graph);

    // CURSOR
    var chartCursor = new AmCharts.ChartCursor();
    chart.addChartCursor(chartCursor);

    // SCROLLBAR
    var chartScrollbar = new AmCharts.ChartScrollbar();
    chart.addChartScrollbar(chartScrollbar);

    chart.creditsPosition = "bottom-right";

    // WRITE
    chart.write("chartdiv");
    $('.cst').click(function(){
      $(".dialog_title").html($(this).parent("div").find("h3").html()+"价格趋势图");
      var number=$(this).attr("data");
      $.ajax({
        url: '/Article/jgzx.html',
        async: false,
        type: 'post',
        data:
          {
            number: number

          },
        success:function(res){
          var data = res.data;  //价格数据
          var times = res.times; //时间
          chartData = [];
          for(var i= times.l_date;i<=times.y_date;i+=86400){
            var js_time = new Date(i * 1000);
            var js_date = js_time.getFullYear()+'-'+date_format((js_time.getMonth() + 1))+'-'+date_format(js_time.getDate());
            var price = 0;
            if(typeof(data[js_date]) != 'undefined') {
              price = data[js_date];
            }
            if(price==0) continue;
            chartData.push({
              date: js_time,
              visits: price
            });
          };
          chart.dataProvider = chartData;
          chart.validateNow();
          chart.validateData();
          $(".dialog").show();
        }
      })

    });
  });
  function date_format(obj){
    /* var y = date.getFullYear();
      var m = date.getMonth()+1;
      if(m.length == 1) m = '0'+m;
      var d = date.getDate();
      if(d.length == 1) d = '0'+d;
      return y + '-' + m + '-' + d; */
    if (obj < 10) return "0" + obj; else return obj;
  }
  function getData(obj,arr){
    for(var m in obj) {
      if(obj[m] == '0.00') continue;
      //遍历底下的数据-价格涨幅
      var js_time = new Date(m * 1000);
      //期数
      var js_date = js_time.getFullYear()+date_format((js_time.getMonth() + 1))+date_format(js_time.getDate());
      if(obj[m-86400] == '0.00') continue;
      var js_zdz=(obj[m]-obj[m-86400]).toFixed(2);//涨跌值
      var js_zdf=((obj[m]-obj[m-86400])*100/obj[m-86400]).toFixed(2);//涨跌幅

      arr.push({
        date: new Date(m*1000),
        visits: obj[m]
      });

    }

  }

}
